<?php
namespace Admin\Controller;

class HomeController extends CommonController {
	public function index(){
        $list = D('Hello');
        $total = $list->count();
        $typelist = $list->getHello();
        $this->assign('total',$total);
        $this->assign('data',$typelist);
        $this->display();
    }

    public function tbshow(){
        $list = D('Tbshow');
        $total = $list->count();
        $typelist = $list->getTbshow();
        $this->assign('total',$total);
        $this->assign('data',$typelist);
        $this->display('tbshow');
    }
	
//===============================添加商品====================================
    public function add(){ 
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;
             $info   =   $upload->upload();// 上传文件
                if(!$info) {        // 上传错误提示错误信息   
                    $this->error($upload->getError());
                }else{  
                    foreach($info as $file){       
                        $img = './Uploads/'.$file['savename'];
                        $image = new \Think\Image;
                        $image->open($img);
                        $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                        $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                        $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);
                    }
                    //接收表单数据
                    $_POST['addtime'] = time();
                    $_POST['picname']  = $file['savename'];
                     $data['picname'] = $file['savename'];
                    $data['price'] = $_POST['price'];
                    $data['descr'] = $_POST['descr'];
                    $data['store'] = $_POST['store'];
                    $data['shop_name'] = $_POST['shop_name'];
                    $data['typeid'] = $_POST['typeid'];
                    if($_POST){
                        $goods = D('goods');
                        $result = $goods->add($_POST);
                        $data['goodsid'] = $result;
                        $list = D('hello');
                        $list->add($data);
                        $this->redirect('index');
                    }
                }
        }else{
               $type = D('Type');
               $list = $type->getAll();
               $this->assign('list',$list);            
               $this->display();
        }
    }
    //删除 此次删除 只能删除hello表 并不会彻底删除商品
    public function del(){
        $id = $_GET['id'];
        
        $result = D('hello');
        $goodsid = $_GET['goodsid'];
        $goods = D('goods');
        $goods->delete($goodsid);
        $result->delete($id);
        $this->redirect('index');
    }
    public function update(){
        if(IS_POST){
            if($_FILES['picname']['name']){
                $data['picname'] = $_POST['oldpicname'];
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;
            $info   =   $upload->upload();// 上传文件
            if(!$info) {        // 上传错误提示错误信息   
                $this->error($upload->getError());
            }else{  
                foreach($info as $file){       
                    $img = './Uploads/'.$file['savename'];
                    $image = new \Think\Image;
                    $image->open($img);
                    $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                    $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                    $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);
                    unlink ('./Uploads/'.$data[picname]);
                    unlink ('./Uploads/s_'.$data[picname]);
                    unlink ('./Uploads/m_'.$data[picname]);
                    unlink ('./Uploads/v_'.$data[picname]);
                    $data['picname'] = $file['savename'];
                }
            }
        }else{
            $data['picname'] = $_POST['oldpicname'];
        }
            //接收表单数据
            $data['id'] = $_POST['id'];
            $data['typeid'] = $_POST['typeid'];
            $data['goods'] = $_POST['goods'];
            $data['shop_name'] = $_POST['shop_name'];
            $data['price'] = $_POST['price'];
            $data['store'] = $_POST['store'];
            $data['descr'] = $_POST['descr'];
            $data['addtime'] = time();
            $type = D('goods');
            $hello = D('hello');
            $type->save($data);
            $hello->save($data);
            $this->redirect('index');
        }else{
            $type = D('Type');
            $list = $type->getType();
            $this->assign('list',$list);
            $id = $_GET['id'];
            $goods = D('goods');
            $data = $goods->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }

   //=======================特别推荐=====================================================
    public function tbs_add(){
        if(IS_POST){
        $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;
             $info   =   $upload->upload();// 上传文件
                if(!$info) {        // 上传错误提示错误信息   
                    $this->error($upload->getError());
                }else{  
                    foreach($info as $file){       
                        $img = './Uploads/'.$file['savename'];
                        $image = new \Think\Image;
                        $image->open($img);
                        $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                        $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                        $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);
                    }
                    //接收表单数据
                    $_POST['addtime'] = time();
                    $_POST['picname']  = $file['savename'];
                    $data['picname'] = $file['savename'];
                    $data['price'] = $_POST['price'];
                    $data['descr'] = $_POST['descr'];
                    $data['store'] = $_POST['store'];
                    $data['goods'] = $_POST['goods'];
                    $data['shop_name'] = $_POST['shop_name'];
                    $data['typeid'] = $_POST['typeid'];
                    if($_POST){
                        $goods = D('goods');
                        $result = $goods->add($_POST);
                        $data['goodsid'] = $result;
                        $list = D('tbshow');
                        $list->add($data);
                        $this->redirect('tbshow');
                    }
                }
            
        }else{
               $type = D('Type');
               $list = $type->getAll();
               $this->assign('list',$list);            
               $this->display();
        }
    }

    public function tbs_del(){
        $id =  $_GET['id'];
        $list = D('tbshow');
        $goodsid = $_GET['goodsid'];
        $goods = D('goods');
        $data = $goods->find($id);
        unlink ('./Uploads/'.$data[picname]);
        unlink ('./Uploads/s_'.$data[picname]);
        unlink ('./Uploads/m_'.$data[picname]);
        unlink ('./Uploads/v_'.$data[picname]);
        $goods->delete($goodsid);
        $list->delete($id);
        $this->redirect('tbshow');
    }

    public function tbs_update(){
        if(IS_POST){
            if($_FILES['picname']['name']){
                $data['picname'] = $_POST['oldpicname'];
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;
            $info   =   $upload->upload();// 上传文件
            if(!$info) {        // 上传错误提示错误信息   
                $this->error($upload->getError());
            }else{  
                foreach($info as $file){       
                    $img = './Uploads/'.$file['savename'];
                    $image = new \Think\Image;
                    $image->open($img);
                    $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                    $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                    $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);

                    unlink ('./Uploads/'.$data[picname]);
                    unlink ('./Uploads/s_'.$data[picname]);
                    unlink ('./Uploads/m_'.$data[picname]);
                    unlink ('./Uploads/v_'.$data[picname]);
                    $data['picname'] = $file['savename'];
                }
            }
        }else{
            $data['picname'] = $_POST['oldpicname'];
        }
                
            //接收表单数据
            $data['id'] = $_POST['id'];
            $data['typeid'] = $_POST['typeid'];
            $data['shoper'] = $_POST['shoper'];
            $data['shop_name'] = $_POST['shop_name'];
            $data['descr'] = $_POST['descr'];
            $data['addtime'] = time();
            $type = D('tbshow');
            $goods = D('goods');
            $goods->save($data);
            $type->save($data);
            $this->redirect('tbshow');
        }else{

            $type = D('Type');
            $list = $type->getType();
            $this->assign('list',$list);
            $id = $_GET['id'];
            $goods = D('goods');
            $data = $goods->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    
    }
    //===============================首页轮播==================================================
    public function vido(){
        $list = D('vido');
        $total = $list->count();

        $typelist = $list->getVido();
        $this->assign('total',$total);
        $this->assign('data',$typelist);
        $this->display();
       
    }

    public function vido_del(){
        $id =  $_GET['id'];
        $list = D('vido');
        $goodsid = $_GET['goodsid'];
        $goods = D('goods');
        $data = $goods->find($id);
        unlink ('./Uploads/'.$data[picname]);
        unlink ('./Uploads/s_'.$data[picname]);
        unlink ('./Uploads/m_'.$data[picname]);
        unlink ('./Uploads/v_'.$data[picname]);
        $goods->delete($goodsid);
        $list->delete($id);
        $this->redirect('vido');
    }

    public function vido_add(){
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;

             $info   =   $upload->upload();// 上传文件
             //dump($info);die();
             
                if(!$info) {        // 上传错误提示错误信息   
                    $this->error($upload->getError());
                }else{  
                    foreach($info as $file){       
                        $img = './Uploads/'.$file['savename'];
                        $image = new \Think\Image;
                        $image->open($img);
                        $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                        $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                        $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);
                    }
                    //接收表单数据
                    
                    $_POST['addtime'] = time();
                    $_POST['picname']  = $file['savename'];
                    $data['picname'] = $file['savename'];
                    $data['typeid'] = $_POST['typeid'];
                    if($_POST){
                        $goods = D('goods');
                        $result = $goods->add($_POST);
                        $data['goodsid'] = $result;
                        $list = D('vido');
                        $list->add($data);
                        $this->redirect('vido');
                    }
                }
            
        }else{
               $type = D('Type');
               $list = $type->getType();
               $this->assign('list',$list);            
               $this->display();
        }
    }
}

